package cn.tedu.csmall.product.controller;


import cn.tedu.csmall.product.pojo.dto.AlbumAddNewDto;
import cn.tedu.csmall.product.pojo.dto.AlbumUpdateDto;
import cn.tedu.csmall.product.pojo.vo.AlbumListItemVo;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVo;
import cn.tedu.csmall.product.service.AlbumService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/album")
@Api(tags = "01. 相册管理模块")
@Validated
@Slf4j
public class AlbumController {

    @Autowired
    AlbumService service;

    /**
     * 添加相册
     *
     * @param addNewDto
     * @return
     */
    @PostMapping("/add-new")
    @ApiOperation("添加相册")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid AlbumAddNewDto addNewDto) {
        log.debug("AlbumAddNewDto + {}", addNewDto);
        service.addNew(addNewDto);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除相册")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "相册ID", required = true, dataType = "long"),
    })
    public JsonResult<Void> delete(@PathVariable @Range(min = 1) Long id) {
        service.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改相册详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "相册ID", required = true, dataType = "long")
    })
    public JsonResult<Void> updateInfoById(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id,
                                           @Valid AlbumUpdateDto albumUpdateDTO) {
        log.debug("开始处理【修改相册详情】的请求，ID：{}，新数据：{}", id, albumUpdateDTO);
        service.updateInfoById(id, albumUpdateDTO);
        return JsonResult.ok();
    }

    @GetMapping("")
    @ApiOperation("获取列表")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<AlbumListItemVo>> list() {
        List<AlbumListItemVo> list = service.list();
        return JsonResult.ok(list);
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询相册详情")
    @ApiOperationSupport(order = 410)
    public JsonResult<AlbumStandardVo> getStandardById(
            @PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【根据ID查询相册详情】的请求，参数：{}", id);
        AlbumStandardVo queryResult = service.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    @PostMapping("/rebuild-cache")
    @ApiOperation("重建相册缓存")
    @ApiOperationSupport(order = 900)
    public JsonResult<Void> rebuildCache() {
        service.rebuildCache();
        return JsonResult.ok();
    }
}
